<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Service\Pools;

use App\Models\DividendAccount;
use App\Models\DividendPool;
use App\Models\PointPriceHistory;
use App\Models\PointSetting;
use App\Service\BaseService;

class DividendService extends BaseService
{
    /**
     * 注入资金.
     * @param $amount
     * @param mixed $type
     */
    public function injectFunds($amount = 0, $type = 1)
    {
        $bonus_pool = PointSetting::query()->first();
        switch ($type) {
            case 1:
                //拼团
                $point_bonus_pool = $bonus_pool->group_point_bonus_pool; //拼团积分入池比例
                $bean_bonus_pool = $bonus_pool->group_bean_bonus_pool; //拼团通证入池比例
                break;
            case 2:
                //新零售
                $point_bonus_pool = $bonus_pool->product_point_bonus_pool; //新零售积分入池比例
                $bean_bonus_pool = $bonus_pool->product_bean_bonus_pool; //新零售通证入池比例
                break;
        }
        #奖金
        $point_bonus = bcmul($amount, $point_bonus_pool, 2);
        $bean_bonus = bcmul($amount, $bean_bonus_pool, 2);
        #总数
        $total = bcadd($bean_bonus, $point_bonus);
        #实际 扣除储备金
        $br = bcmul($bonus_pool->reserve_pool, 100, 2);
        $point_bonus = splitAmountByPercentage($point_bonus, $br);
        $bean_bonus = splitAmountByPercentage($bean_bonus, $br);

        #储备池
        $reserve_pool = bcadd($point_bonus['a'], $bean_bonus['a'], 2); //入储备池比例
        $dp = $this->getDividendPool();
        $dp->total_amount += $total;
        $dp->reserve_amount += $reserve_pool;
        $dp->bean_released_amount += $bean_bonus['b'];
        $dp->point_released_amount += $point_bonus['b'];
        $dp->total_number += 1;
        $dp->save();
        ReserveService::make()->injectFunds($reserve_pool);
    }

    /**
     * 积分价格
     * 昨日分红池/总购买商品销售额.
     */
    public function Conversion(): array
    {
        $dp = $this->getSubDividendPool();
        $point_total_pool_amount = $dp->point_released_amount; //积分释放金额
        $bean_total_pool_amount = $dp->bean_released_amount; //通证释放金额

        $pbs = DividendAccount::query();
        $point_amount = $pbs->sum('point_amount');
        $bean_amount = $pbs->sum('bean_amount');

        $point_price = bcdiv($point_total_pool_amount, $point_amount, 5);   //当前积分价格
        $bean_price = bcdiv($bean_total_pool_amount, $bean_amount, 5);   //当前通证价格

        $data = [
            'point_price' => $point_price,
            'bean_price' => $bean_price,
            'total_point_pool_amount' => $point_total_pool_amount,
            'total_point_member_points' => $point_amount,
            'total_bean_pool_amount' => $bean_total_pool_amount,
            'total_bean_member_points' => $bean_amount,
        ];
        PointPriceHistory::query()->firstOrCreate([
            'date' => now()->subDay()->toDateString(),
        ], $data);

        return [$point_price, $bean_price];
    }

    /**
     * 每日初始化分红池.
     * @param null|mixed $date
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|void
     */
    protected function initDividend($date = null)
    {
        if (is_null($date)) {
            $date = now()->toDateString();
        }
        return DividendPool::query()->firstOrCreate([
            'date' => $date,
        ]);
    }

    /**
     * 获得红利池.
     * @return null|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object
     */
    protected function getDividendPool()
    {
        $model = DividendPool::query()->where('date', now()->toDateString())->first();
        if (blank($model)) {
            $model = $this->initDividend(now()->toDateString());
        }
        return $model;
    }

    public function getSubDividendPool()
    {
        $model = DividendPool::query()->where('date', now()->subDay()->toDateString())->first();
        if (blank($model)) {
            $model = $this->initDividend(now()->subDay()->toDateString());
        }
        return $model;
    }
}
